package com.yeshen.pmtea.ui;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

import com.yeshen.pmtea.R;
import com.yeshen.pmtea.beans.DBHelper;
import com.yeshen.pmtea.beans.News;
import com.yeshen.pmtea.beans.Urls;
import com.yeshen.pmtea.utils.HttpUtils;
import com.yeshen.pmtea.utils.SqliteUtils;

import org.json.JSONException;
import org.json.JSONObject;

public class NewsItemContentActivity extends AppCompatActivity {
    private WebView webView;
    private String url = Urls.CONTENT_URL;
    private News news;
    private String wap_content;
    private DBHelper dbHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_item_content2);
        getSupportActionBar().hide();
        initView();
    }
    private void initView() {
        webView = (WebView) findViewById(R.id.webView);
        StringBuilder sb = new StringBuilder();
        news = (News) getIntent().getSerializableExtra("key");
        TextView tvTitle = (TextView) findViewById(R.id.title_collect);
        TextView tvTime = (TextView) findViewById(R.id.time_collect);
        tvTitle.setText(news.getTitle());
        tvTime.setText(news.getCreate_time());
        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);//设置支持javascript脚本
        settings.setBuiltInZoomControls(true);//显示缩放的工具
        settings.setSupportZoom(true);//页面可以缩放
        new NewsJsonAsyncTask(webView).execute(url);

    }
    //点击按钮返回到之前的activity,这里直接结束当前的activity
    public  void onBack(View view){
        finish();
    }
    //添加收藏的点击事件,toast提示收藏成功
    public void onCollect(View view){
        //数据传回之后先判断该页面是否已经添加到收藏中,没有就添加到数据库中,有就提示
        dbHelper = new DBHelper(NewsItemContentActivity.this);
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from collect where id = ?",new String[]{news.getId()});
        if (cursor.moveToNext()) {
            Toast.makeText(NewsItemContentActivity.this, "已收藏", Toast.LENGTH_SHORT).show();
            return;
        }

        SqliteUtils.insertToSQL(NewsItemContentActivity.this,news.getId(),news.getTitle(),news.getSource(),news.getWap_thumb(),news.getCreate_time(),news.getNickname());
        Toast.makeText(NewsItemContentActivity.this, "收藏成功", Toast.LENGTH_SHORT).show();

    }

    class NewsJsonAsyncTask extends AsyncTask<String,Void,String> {
        private WebView webView;
        public NewsJsonAsyncTask(WebView webView){
            this.webView = webView;
        }
        @Override
        protected String doInBackground(String... strings) {
            String json = HttpUtils.getNewsJson(strings[0],news.getId());
            Log.d("flag", "---------------doInBackground: " +json);
            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(json);
                JSONObject jsonObject1 = jsonObject.getJSONObject("data");

                wap_content = jsonObject1.getString("wap_content");
                return wap_content;

            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);

            //webView.loadUrl(url),表示在当前页面展示
            webView.setWebViewClient(new WebViewClient());
            this.webView.loadDataWithBaseURL(null,s,"text/html", "UTF-8", null);
        }
    }
}
